﻿/* 
 Copyright (C) 2011  Amblox Project

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;

using Amblox.Encoding;
using Amblox.Game;
using Amblox.Net;

namespace Amblox.Handlers
{
    public partial class Handler
    {
        HabboClient Client;
        Dictionary<int, Request> Packets;
        string Packet;

        public Habbo Habbo
        {
            set
            {
                if(Client != null)
                    Client.Habbo = value;
            }
            get
            {
                if (Client == null)
                    return null;

                return Client.Habbo;
            }
        }

        public NetConnection Connection
        {
            set
            {
                if (Client != null)
                    Client.Connection = value;
            }
            get
            {
                if (Client == null)
                    return null;

                return Client.Connection;
            }
        }

        public Messenger Messenger
        {
            set
            {
                if (Client != null)
                    Client.Messenger = value;
            }
            get
            {
                if (Client == null)
                    return null;

                return Client.Messenger;
            }
        }

        public delegate void Request();

        public bool Destroyed
        {
            get
            {
                if (Client == null && Packets == null && Packet == null)
                    return true;
                else
                    return false;
            }
        }

        public Handler(HabboClient Client)
        {
            this.Client = Client;
            this.Packets = new Dictionary<int, Request>();
            this.Register();
        }

        public void ElaboratePacket(string Packet)
        {
            if (Destroyed)
                return;

            this.Packet = Packet;

            int header = JeaxEncoding.decodeB64(Packet.Substring(0, 2));
            if (Packets.ContainsKey(header))
                Packets[header].Invoke();
            else
                Log.WriteLine("Pacchetto sconosciuto header: " + header + " data: " + Packet);
        }

        public void Register()
        {
            RegisterHandshake();
            RegisterGlobal();
        }

        public void Destroy()
        {
            if (Destroyed)
                return;

            try
            {
                Packets.Clear();
            }
            catch { }

            try
            {
                Client.Handler = null;
                Client.Destroy();
            }
            catch { }

            Packets = null;
            Packet = null;
            Client = null;
        }
    }
}
